package com.goods.business.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.SupplierConverter;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    //查询分页
    @Override
    public PageVO<SupplierVO> findSupplierList(Integer pageNum, Integer pageSize,SupplierVO supplierVO) {
        SupplierConverter supplierConverter= new SupplierConverter();
        PageHelper.startPage(pageNum,pageSize);
        Example example = new Example(Supplier.class);
        String address = supplierVO.getAddress();//省市区
        String name = supplierVO.getName();//具体地点
        String contact = supplierVO.getContact();//联系人
        Example.Criteria criteria = example.createCriteria();
        if (address != null && !"".equals(address)){
            criteria.andLike("address","%"+address+"%");
        }
        if (name != null && !"".equals(name)){
            criteria.andLike("name","%"+name+"%");
        }
        if (contact != null && !"".equals(contact)){
            criteria.andEqualTo("contact",contact);
        }
        List<Supplier> suppliers = supplierMapper.selectByExample(example);
        List<SupplierVO> supplierVOS = supplierConverter.converterToSupplierVOList(suppliers);
        PageInfo<Supplier> info = new PageInfo<>(suppliers);
        return new PageVO<>(info.getTotal(),supplierVOS);
    }
    //新增
    @Override
    public void add(SupplierVO supplierVO) {
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setCreateTime(new Date());
        supplier.setModifiedTime(new Date());
        supplierMapper.insert(supplier);
    }
    //修改回显
    @Override
    public SupplierVO edit(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);
        return supplierVO;
    }
    //修改确认
    @Override
    public Object update(Long id, SupplierVO supplierVO) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(supplierVO,supplier);
        supplier.setModifiedTime(new Date());
        return supplier;
    }
    //删除
    @Override
    public Object delete(Long id) {
        Supplier supplier = supplierMapper.selectByPrimaryKey(id);
        return supplierMapper.deleteByPrimaryKey(supplier);
    }



}
